package org.flexthinker.module.impl
{
    import mx.events.ModuleEvent;

    import mx.logging.Log;

    import mx.logging.targets.TraceTarget;

    import org.flexunit.async.Async;
    import org.hamcrest.assertThat;
    import org.hamcrest.number.greaterThan;
    import org.hamcrest.object.equalTo;

    public class CompositeModuleDescriptorLoaderTest
    {
        //to be used when tested from IJ IDE
//        private static const MODULE_URL:String = "test-classes/test/ButtonModule.swf";
//        private static const MODULE2_URL:String = "test-classes/test/ButtonModule2.swf";
//        private static const MODULE_SKIN_URL:String = "test-classes/test/Skin1_buttonModule.swf";
//        private static const MODULE_SKIN2_URL:String = "test-classes/test/Skin2_buttonModule.swf";

        // to the used when tested with maven
        private static const MODULE_URL:String = "test/ButtonModule.swf";
        private static const MODULE2_URL:String = "test/ButtonModule2.swf";
        private static const MODULE_SKIN_URL:String = "test/Skin1_buttonModule.swf";
        private static const MODULE_SKIN2_URL:String = "test/Skin2_buttonModule.swf";

        private var loader:CompositeModuleDescriptorLoader;

        private var descriptor:CompositeModuleDescriptor;

        private var progressData:Array = [];


        public function CompositeModuleDescriptorLoaderTest()
        {
        }

        [Before]
        public function init():void
        {
            descriptor = new CompositeModuleDescriptor();
            descriptor.addChild(getModuleDescriptor(MODULE_URL, MODULE_SKIN_URL));
            descriptor.addChild(getModuleDescriptor(MODULE2_URL, MODULE_SKIN2_URL));

            Log.addTarget(new TraceTarget());
        }

        private function getModuleDescriptor(url:String, stylesheetUrl:String):ModuleDescriptor
        {
            var d:ModuleDescriptor = new ModuleDescriptor(url);
            d.addDependency(new StylesheetDescriptor(stylesheetUrl));
            return d;
        }

        [Test(async,timeout="6000")]
        public function testCompositeModuleLoading():void
        {
            loader = new CompositeModuleDescriptorLoader(descriptor);

            Async.handleEvent(this, loader, ModuleEvent.READY, readyHandler, 6000);
            loader.addEventListener(ModuleEvent.ERROR, errorHandler);
            loader.addEventListener(ModuleEvent.PROGRESS, progressHandler);

            assertThat("descriptors length is wrong", loader.numDescriptors, equalTo(2));

            loader.load();
        }

        private function readyHandler(event:ModuleEvent, param:Object):void
        {
            var lastProgressData:Object = progressData[ progressData.length - 1 ];
            assertThat("BytesLoaded are wrong", lastProgressData.bytesLoaded, greaterThan(2060000));
            assertThat("BytesTotal are wrong", lastProgressData.bytesTotal, greaterThan(2060000));
        }

        private function errorHandler(event:ModuleEvent):void
        {
            assertThat("module should have loaded", event.errorText, equalTo("Error is:"));
        }

        private function progressHandler(event:ModuleEvent):void
        {
//           trace("PROGRESS: " + event.bytesLoaded + ":" + event.bytesTotal);
            progressData.push({bytesLoaded:event.bytesLoaded, bytesTotal:event.bytesTotal });
        }

    }
}